package net.wnb.mineclearance.ui;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import net.wnb.mineclearance.helper.GameManager;
import net.wnb.mineclearance.model.Game;

import wnb.util.io.FileSource;
import wnb.util.io.ImageManager;
import wnb.util.io.Terminal;
import wnb.util.ui.AnimationPane;
import wnb.util.ui.ImageAnimation;

public class MenuPane extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String WELCOM_IMAGE_FILE_NAME = "res/images/welcom.png";
	private static final int DEFAULT_DELAY = 50;
	private static final int PREFERRED_SIZE = 240;
	private static final int BUTTON_H = 32;
	private static final int DEFAULT_WIDTH = 320;
	private static final int DEFAULT_HEIGHT = 480;
	
	AnimationPane pane;
	JButton btnNew;
	JButton btnContinue;
	JButton btnHelp;
	JButton btnAbout;
	JButton btnExit;
	
	public MenuPane(){
		init();
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(pane);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(btnNew);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(btnContinue);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(btnHelp);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(btnAbout);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(btnExit);
		add(Box.createRigidArea(new Dimension(0, 10)));
		
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		validate();
		pane.start();
	}
	
	private void init(){
		try{
			Image image = ImageManager.getImage(WELCOM_IMAGE_FILE_NAME, FileSource.FILE_SYSTEM);
			ImageAnimation animation = new ImageAnimation(image, 2, 4);
			pane = new AnimationPane(animation, DEFAULT_DELAY, true);
			btnNew = new JButton("新游戏");
			btnContinue = new JButton("继续游戏");
			btnHelp = new JButton("帮助");
			btnAbout = new JButton("关于");
			btnExit = new JButton("退出");
			
			pane.setMaximumSize(new Dimension(PREFERRED_SIZE, PREFERRED_SIZE));
			btnNew.setMaximumSize(new Dimension(PREFERRED_SIZE, BUTTON_H));
			btnContinue.setMaximumSize(new Dimension(PREFERRED_SIZE, BUTTON_H));
			btnHelp.setMaximumSize(new Dimension(PREFERRED_SIZE, BUTTON_H));
			btnAbout.setMaximumSize(new Dimension(PREFERRED_SIZE, BUTTON_H));
			btnExit.setMaximumSize(new Dimension(PREFERRED_SIZE, BUTTON_H));
			
			btnNew.addActionListener(this);
			btnAbout.addActionListener(this);
			btnContinue.addActionListener(this);
			btnHelp.addActionListener(this);
			btnExit.addActionListener(this);
			
			pane.setAlignmentX(CENTER_ALIGNMENT);
			btnNew.setAlignmentX(CENTER_ALIGNMENT);
			btnAbout.setAlignmentX(CENTER_ALIGNMENT);
			btnHelp.setAlignmentX(CENTER_ALIGNMENT);
			btnContinue.setAlignmentX(CENTER_ALIGNMENT);
			btnExit.setAlignmentX(CENTER_ALIGNMENT);
			
			if(!GameManager.savedGameExists()){
				btnContinue.setEnabled(false);
			}
		}catch(IOException ex){
			Terminal.writeLine(ex.getMessage());
			ex.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		Object source = event.getSource();
		if(source.equals(btnNew)){
			SelectionPane selectionPane = new SelectionPane();
			MainWindow win = MainWindow.getInstance();
			win.setPane(selectionPane);
			win.validate();
		}else if(source.equals(btnContinue)){
			Game game = GameManager.read();
			if(game == null){
				return;
			}
			
			GamePane pane = new GamePane(game);
			MainWindow win = MainWindow.getInstance();
			win.setPane(pane);
			win.validate();
			game.start();
		}else if(source.equals(btnExit)){
			System.exit(0);
		}else if(source.equals(btnHelp)){
			HelpPane pane = new HelpPane();
			MainWindow win = MainWindow.getInstance();
			win.setPane(pane);
			win.validate();
		}else if(source.equals(btnAbout)){
			AboutPane pane = new AboutPane();
			MainWindow win = MainWindow.getInstance();
			win.setPane(pane);
			win.validate();
		}
	}
}
